package com.ego.item.controller;

import com.ego.common.utils.PageResult;
import com.ego.item.bo.SpuBo;
import com.ego.item.pojo.Sku;
import com.ego.item.pojo.SpuDetail;
import com.ego.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询SPU
     *
     * @param pageNo
     * @param pageSize
     * @param key
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "rows", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key) {
        PageResult<SpuBo> pageResult = this.goodsService.querySpuByPageAndSort(pageNo, pageSize,
                saleable, key);
        if (pageResult == null || pageResult.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> getCurSpu(@PathVariable(value = "id") Long id) {
        SpuDetail spuDetail = goodsService.getCurSpuDetailById(id);
        if (spuDetail == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } else {
            return ResponseEntity.ok(spuDetail);
        }
    }

    @GetMapping("skus")
    public ResponseEntity<List<Sku>> getCurSpuSkus(@RequestParam("spuId") Long spuId) {
        List<Sku> skus = goodsService.getCurSpuSkusById(spuId);
        if (skus == null || skus.size() == 0) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } else {
            return ResponseEntity.ok(skus);
        }
    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
        goodsService.updateGoods(spuBo);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping
    public ResponseEntity<Void> DeleteGoods(@RequestParam("id") Long id) {
        goodsService.deleteCurGoodById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/id")
    public SpuBo queryGoodsById(@RequestParam(value = "spuId", required = false) Long spuId) {
        return goodsService.queryGoodsById(spuId);
    }

    @GetMapping("/sku")
    public Sku getSkuBySkuId(@RequestParam("skuId") Long skuId) {
        return goodsService.getSkuBySkuId(skuId);
    }


}
